package com.loren.tetris.windows.ui;

import com.loren.tetris.data.GameData;
import com.loren.tetris.data.constant.DataConstant;
import com.loren.tetris.data.entity.DiskInfo;
import com.loren.tetris.utils.AssistiveUtils;

import java.awt.*;
import java.math.BigDecimal;

public class BoxRecord extends Box {

    public BoxRecord(int boxX, int boxY, int boxW, int boxH, GameData gameData) {
        super(boxX, boxY, boxW, boxH, gameData);
    }

    @Override
    protected void gameAction(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        this.drawBoxTitle(g2d, "本地记录");
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g2d.setFont(new Font(AssistiveUtils.getFontFamily(), Font.PLAIN, 16));
        for (int i = 0; i < this.getGameData().getDiskInfoList().size(); i++) {
            DiskInfo diskInfo = this.getGameData().getDiskInfoList().get(i);
            int x = this.getBoxX() + 10;
            int y = this.getBoxY() + 45 + 50 * i;
            int w = this.getBoxW() - 20;
            g2d.setColor(DataConstant.COLOR_WHITE);
            g2d.drawRect(x, y, w, DataConstant.PROGRESS_BAR_HEIGHT);
            g2d.drawString(diskInfo.getName(), x + 10, y + 20);
            g2d.drawString(String.valueOf(diskInfo.getScore()), x + 210, y + 20);
            g2d.setColor(DataConstant.PROGRESS_BAR_COLOR);
            int progressWidth = this.calculateProgress(this.getGameData().getGameScore(), diskInfo.getScore(), w);
            g2d.fillRect(x, y, progressWidth, DataConstant.PROGRESS_BAR_HEIGHT);
        }
    }

    private int calculateProgress(int score, int diskScore, int width) {
        if (score == 0) {
            return 0;
        }
        if (diskScore == 0) {
            return width;
        }
        BigDecimal proportion = BigDecimal.valueOf(score / (diskScore * 1.0));
        double tempWidth = proportion.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() * width;
        return Math.min((int) tempWidth, width);
    }
}
